// pages/main/index.js
var QR = require("../../utils/qrcode.js");
var OPEN_ID = ''//储存获取到openid  
var SESSION_KEY = ''//储存获取到session_key  
var Url = 'https://www.xbbsport.com/'
Page({
  data: {
    canvasHidden: false,
    maskHidden: true,
    imagePath: '',
    placeholder: 'http://wxapp-union.com',//默认二维码生成文本
    openId: ''
  },
  onLoad: function (options) {
    console.log("onLoad")

    // 页面初始化 options为页面跳转所带来的参数
    var size = this.setCanvasSize();//动态设置画布大小
    var initUrl = this.data.placeholder;
    // this.createQrCode(initUrl, "mycanvas", size.w, size.h);
    var that = this;
    // that.openId
    OPEN_ID = options.openId;
    console.log("index--onLoad+" + OPEN_ID)
    that.createQrCode(OPEN_ID, "mycanvas", size.w, size.h);

    // wx.getUserInfo({
    //   success: function (res) {
    //     console.log(res)
    //     // that.setData({
    //     //   placeholder: res.userInfo,
    //     // });
    //     // that.createQrCode(that.data.placeholder, "mycanvas", size.w, size.h);
    //   }
    // })

  },

  // getPhoneNumber: function (e) {
  //   console.log(e.detail.errMsg)
  //   console.log(e.detail.iv)
  //   console.log(e.detail.encryptedData)
  //   if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  //     wx.showModal({
  //       title: '提示',
  //       showCancel: false,
  //       content: '未授权',
  //       success: function (res) { }
  //     })
  //   } else {
  //     wx.showModal({
  //       title: '提示',
  //       showCancel: false,
  //       content: '同意授权',
  //       success: function (res) { }
  //     })
  //   }
  // },  
  onReady: function () {
    console.log("onReady")

  },
  onShow: function () {
    console.log("onShow")
    console.log("openId="+OPEN_ID)
    this.checkUser(OPEN_ID)

    // 页面显示
  },
  onHide: function () {
    console.log("onHide")

    // 页面隐藏
  },

  onUnload: function () {
    console.log("onUnload")
    // 页面关闭

  },
  onShareAppMessage: function () {
    return {
      title: '花儿美学机构',
      desc: '美美的优惠券',
      path: 'pages/main/index'
    }
  },
  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 686;//不同屏幕下canvas的适配比例；设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width;//canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => { this.canvasToTempImage(); }, 1000);

  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  checkUser: function (openId) {
    wx.request({
      url: Url + "coupon/isUse?openId=" + openId,
      success: function (res) {
        console.log("Check=" + res.data.Data)
        var titleString = res.data.Data==1?"您的体验券已使用过":"领取成功"
        wx.showToast({
          title: titleString,
        })
      }
    })

  },

  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath;
    console.log(img);
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  formSubmit: function (e) {
    var that = this;
    var url = e.detail.value.url;
    console.log("url=" + url)
    that.setData({
      maskHidden: false,
    });
    wx.showToast({
      title: '生成中...',
      icon: 'loading',
      duration: 2000
    });
    var st = setTimeout(function () {
      wx.hideToast()
      var size = that.setCanvasSize();
      //绘制二维码
      that.createQrCode(url, "mycanvas", size.w, size.h);
      that.setData({
        maskHidden: true
      });
      clearTimeout(st);
    }, 2000)

  }

})

// wx.login({
//   success: function (res) {
//     console.log(res)
//     if (res.code) {
//       wx.showToast({
//         title: '登录成功提示',
//       })
//       wx.request({
//         url: 'http://47.100.2.17:8080/coupon/getOpenId?code=' + res.code,
//         header: {
//           'content-type': 'json'
//         },
//         method: 'GET',
//         success: function (res) {
//           wx.showToast({
//             title: '请求openId成功',
//           })
//           OPEN_ID = res.data.openid
//           wx.request({
//             url: "http://47.100.2.17:8080/coupon/isAdmin?openId=" + res.data.openid,
//             header: {
//               'content-type': 'json'
//             },
//             method: 'GET',
//             success: function (res) {
//               wx.showToast({
//                 title: '验证admin成功',
//               })
//               console.log(res.data.Data)
//               if (res.data.Data == 1) {
//                 // wx.navigateTo({
//                 //   url: 'check'
//                 // })
//               }
//               console.log(res.data)
//               that.createQrCode(OPEN_ID, "mycanvas", size.w, size.h);
//             }
//           })
//         },
//         fail: function (res) {
//           wx.showToast({
//             title: '请求openId失败' + res.data,
//           })

//         },
//         complete: function (res) {
//           console.log(res.data)
//           wx.showToast({
//             title: "?" + res.data,
//           })
//         }

//       })
//     } else {
//       wx.showToast({
//         title: '提示',
//         content: '登录失败'
//       })
//     }

//   },
//   complete: function (res) {

//   }

// })